﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Windows.Forms;
using System.Globalization;
using System.Data.SqlClient;

namespace GenCode{
    internal class FormDesigner : MySqlProvider
    {
        public static void FillGrid(string databasename, string tablename, ref DataTable dt,
            DevExpress.XtraGrid.GridControl grd)
        {
            DataTable dtColumn;
            dtColumn = GetDataTable1(databasename, tablename);
            CreateTableStructure(ref dt);
            for (int i = 0; i < dtColumn.Rows.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["ColumnName"] = dtColumn.Rows[i]["COLUMN_NAME"];
                dr["Hidden"] = 0;
                dr["Repository"] = 0;
                dt.Rows.Add(dr);
            }
            grd.DataSource = dt;
        }

        public static DataTable GetDataTable1(string databasename, string tablename)
        {
            DataTable dtTemp = null;

            string Query =
                @"SELECT *
                           FROM INFORMATION_SCHEMA.Columns
                           WHERE Table_Name='" + tablename + "'";

            string conn = "";
            if (Global1.win)
            {
                conn = Global1.QuyenWin(databasename);
            }
            else
                conn = Global1.QuyenAuthen(databasename);
            var con =
                new SqlConnection(conn);
            var cmd = new SqlCommand(Query, con);

            try
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                    //Open();
                }
                dtTemp = new DataTable();
                dtTemp.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));
                return dtTemp;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                con.Close();
                dtTemp.Dispose();
            }
        }

        public static string CodeFormDesigner(string databasename, string tablename, string name_space, ref DataTable dt){
            string ketqua = "";

            ketqua = ketqua + "namespace " + name_space + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "partial class " + FRMTableName(tablename) + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "/// <summary>" + Environment.NewLine;
            ketqua = ketqua + "/// Required designer variable." + Environment.NewLine;
            ketqua = ketqua + "/// </summary>" + Environment.NewLine;
            ketqua = ketqua + "private System.ComponentModel.IContainer components = null;" + Environment.NewLine;
            ketqua = ketqua + Environment.NewLine;
            ketqua = ketqua + "/// <summary>" + Environment.NewLine;
            ketqua = ketqua + "/// Clean up any resources being used." + Environment.NewLine;
            ketqua = ketqua + "/// </summary>" + Environment.NewLine;
            ketqua = ketqua +
                     "/// <param name=\"disposing\">true if managed resources should be disposed; otherwise, false.</param>" +
                     Environment.NewLine;
            ketqua = ketqua + "protected override void Dispose(bool disposing)" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "if (disposing && (components != null))" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "components.Dispose();" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "base.Dispose(disposing);" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + Environment.NewLine;
            ketqua = ketqua + "#region Windows Form Designer generated code" + Environment.NewLine;
            ketqua = ketqua + Environment.NewLine;
            ketqua = ketqua + "/// <summary>" + Environment.NewLine;
            ketqua = ketqua + "/// Required method for Designer support - do not modify" + Environment.NewLine;
            ketqua = ketqua + "/// the contents of this method with the code editor." + Environment.NewLine;
            ketqua = ketqua + "/// </summary>" + Environment.NewLine;
            ketqua = ketqua + "private void InitializeComponent()" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "this.components = new System.ComponentModel.Container();" + Environment.NewLine;
            ketqua = ketqua +
                     "System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(" +
                     FRMTableName(tablename) + "));" + Environment.NewLine;
       
            ketqua = ketqua + "this.mnuMenu = new System.Windows.Forms.ToolStrip();" + Environment.NewLine;
            ketqua = ketqua + "this.btnNew = new System.Windows.Forms.ToolStripButton();" + Environment.NewLine;
            ketqua = ketqua + "this.btnEdit = new System.Windows.Forms.ToolStripButton();" + Environment.NewLine;
            ketqua = ketqua + "this.btnDelete = new System.Windows.Forms.ToolStripButton();" + Environment.NewLine;
            ketqua = ketqua + "this.btnSave = new System.Windows.Forms.ToolStripButton();" + Environment.NewLine;
            ketqua = ketqua + "this.btnCancel = new System.Windows.Forms.ToolStripButton();" + Environment.NewLine;
            ketqua = ketqua + "this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();" +
                     Environment.NewLine;
            ketqua = ketqua + "this.btnClose = new System.Windows.Forms.ToolStripButton();" + Environment.NewLine;
            ketqua = ketqua + "this.gridControl1 = new DevExpress.XtraGrid.GridControl();" + Environment.NewLine;
            ketqua = ketqua + "this.gridView1 = new DevExpress.XtraGrid.Views.Grid.GridView();" + Environment.NewLine;
            //colName

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ketqua = ketqua + "this." + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) +
                         " = new DevExpress.XtraGrid.Columns.GridColumn();" + Environment.NewLine;
                switch (Convert.ToInt16(dt.Rows[i]["Repository"]))
                {
                    case 0: //không dùng
                        break;
                    case 1: //RepositoryItemDateEdit
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 " = new DevExpress.XtraEditors.Repository.RepositoryItemDateEdit();" +
                                 Environment.NewLine;
                        break;
                    case 2: //RepositoryItemLookUpEdit
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 " = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();" +
                                 Environment.NewLine;
                        break;
                    case 3: //RepositoryItemGridLookUpEdit
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 " = new DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit();" +
                                 Environment.NewLine;
                        break;
                    case 4: //RepositoryItemCalcEdit
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 " = new DevExpress.XtraEditors.Repository.RepositoryItemCalcEdit();" +
                                 Environment.NewLine;
                        break;
                    case 5: //RepositoryItemButtonEdit
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 " = new DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit();" +
                                 Environment.NewLine;
                        break;
                }
            }
            ketqua = ketqua + "this.mnuMenu.SuspendLayout();" + Environment.NewLine;
              ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this.gridControl1)).BeginInit();" +
                     Environment.NewLine;
            ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this.gridView1)).BeginInit();" +
                     Environment.NewLine;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //if (Convert.ToInt16(dt.Rows[i]["Repository"]) > 0)
                //{
                //    ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this.item" +
                //             dt.Rows[i]["ColumnName"].ToString() + ")).BeginInit();" + Environment.NewLine;
                //}
                switch (Convert.ToInt16(dt.Rows[i]["Repository"]))
                {
                    case 0: //không dùng
                        break;
                    case 1: //RepositoryItemDateEdit
                        ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this." +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ")).BeginInit();" +
                                 Environment.NewLine;
                        break;
                    case 2: //RepositoryItemLookUpEdit
                        ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this." +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ")).BeginInit();" +
                                 Environment.NewLine;
                        break;
                    case 3: //RepositoryItemGridLookUpEdit
                        ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this." +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ")).BeginInit();" +
                                 Environment.NewLine;
                        ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this.grv" +
                                 dt.Rows[i]["ColumnName"].ToString() + ")).BeginInit();" + Environment.NewLine;
                        break;
                    case 4: //RepositoryItemCalcEdit
                        ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this." +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ")).BeginInit();" +
                                 Environment.NewLine;
                        break;
                    case 5: //RepositoryItemButtonEdit
                        ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this." +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ")).BeginInit();" +
                                 Environment.NewLine;
                        break;
                }
            }
            ketqua = ketqua + "this.SuspendLayout();" + Environment.NewLine;

            ketqua = ketqua + "// " + Environment.NewLine;
            ketqua = ketqua + "// mnuMenu" + Environment.NewLine;
            ketqua = ketqua + "// " + Environment.NewLine;
            ketqua = ketqua + "this.mnuMenu.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)" + Environment.NewLine;
            ketqua = ketqua + "| System.Windows.Forms.AnchorStyles.Right)));" + Environment.NewLine;
            ketqua = ketqua + "this.mnuMenu.AutoSize = false;" + Environment.NewLine;
            ketqua = ketqua + "this.mnuMenu.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(234)))), ((int)(((byte)(234)))), ((int)(((byte)(234)))));" + Environment.NewLine;
            ketqua = ketqua + "this.mnuMenu.Font = new System.Drawing.Font(\"Tahoma\", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));" + Environment.NewLine;
            ketqua = ketqua + "this.mnuMenu.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;" + Environment.NewLine;
            ketqua = ketqua + "this.mnuMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {" + Environment.NewLine;
            ketqua = ketqua + "this.btnNew," + Environment.NewLine;
            ketqua = ketqua + "this.btnEdit," + Environment.NewLine;
            ketqua = ketqua + "this.btnDelete," + Environment.NewLine;
            ketqua = ketqua + "this.btnSave," + Environment.NewLine;
            ketqua = ketqua + "this.btnCancel," + Environment.NewLine;
            ketqua = ketqua + "this.toolStripSeparator1," + Environment.NewLine;
            ketqua = ketqua + "this.btnClose});" + Environment.NewLine;
            ketqua = ketqua + "this.mnuMenu.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Flow;" + Environment.NewLine;
            ketqua = ketqua + "this.mnuMenu.Location = new System.Drawing.Point(0, 0);" + Environment.NewLine;
            ketqua = ketqua + "this.mnuMenu.Name = \"mnuMenu\";" + Environment.NewLine;
            ketqua = ketqua + "this.mnuMenu.RenderMode = System.Windows.Forms.ToolStripRenderMode.Professional;" + Environment.NewLine;
            ketqua = ketqua + "this.mnuMenu.Size = new System.Drawing.Size(863, 42);" + Environment.NewLine;
            ketqua = ketqua + "this.mnuMenu.TabIndex = 22;" + Environment.NewLine;
            ketqua = ketqua + "this.mnuMenu.Text = \"toolStrip2\";" + Environment.NewLine;


            ketqua = ketqua + "// " + Environment.NewLine;
            ketqua = ketqua + "// btnNew" + Environment.NewLine;
            ketqua = ketqua + "// " + Environment.NewLine;
            ketqua = ketqua + "this.btnNew.AutoSize = false;" + Environment.NewLine;
            ketqua = ketqua + "this.btnNew.Name = \"btnNew\";" + Environment.NewLine;
            ketqua = ketqua + "this.btnNew.Size = new System.Drawing.Size(55, 40);" + Environment.NewLine;
            ketqua = ketqua + "this.btnNew.Tag = \"\";" + Environment.NewLine;
            ketqua = ketqua + "this.btnNew.Text = \"&Thêm\";" + Environment.NewLine;
            ketqua = ketqua + "this.btnNew.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;" +
                     Environment.NewLine;
            ketqua = ketqua + "this.btnNew.Click += new System.EventHandler(this.btnNew_Click);" + Environment.NewLine;
            ketqua = ketqua + "// " + Environment.NewLine;
            ketqua = ketqua + "// btnEdit" + Environment.NewLine;
            ketqua = ketqua + "// " + Environment.NewLine;
            ketqua = ketqua + "this.btnEdit.AutoSize = false;" + Environment.NewLine;
            ketqua = ketqua + "this.btnEdit.Name = \"btnEdit\";" + Environment.NewLine;
            ketqua = ketqua + "this.btnEdit.Size = new System.Drawing.Size(55, 40);" + Environment.NewLine;
            ketqua = ketqua + "this.btnEdit.Tag = \"\";" + Environment.NewLine;
            ketqua = ketqua + "this.btnEdit.Text = \"Sửa\";" + Environment.NewLine;
            ketqua = ketqua + "this.btnEdit.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;" +
                     Environment.NewLine;
            ketqua = ketqua + "this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);" + Environment.NewLine;
            ketqua = ketqua + "// " + Environment.NewLine;
            ketqua = ketqua + "// btnDelete" + Environment.NewLine;
            ketqua = ketqua + "// " + Environment.NewLine;
            ketqua = ketqua + "this.btnDelete.AutoSize = false;" + Environment.NewLine;
            ketqua = ketqua + "this.btnDelete.Name = \"btnDelete\";" + Environment.NewLine;
            ketqua = ketqua + "this.btnDelete.Size = new System.Drawing.Size(55, 40);" + Environment.NewLine;
            ketqua = ketqua + "this.btnDelete.Tag = \"\";" + Environment.NewLine;
            ketqua = ketqua + "this.btnDelete.Text = \"Xóa\";" + Environment.NewLine;
            ketqua = ketqua +
                     "this.btnDelete.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;" +
                     Environment.NewLine;
            ketqua = ketqua + "this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);" +
                     Environment.NewLine;
            ketqua = ketqua + "// " + Environment.NewLine;
            ketqua = ketqua + "//  btnSave" + Environment.NewLine;
            ketqua = ketqua + "//  " + Environment.NewLine;
            ketqua = ketqua + "this.btnSave.AutoSize = false;" + Environment.NewLine;
            ketqua = ketqua + "this.btnSave.Name = \"btnSave\";" + Environment.NewLine;
            ketqua = ketqua + "this.btnSave.Size = new System.Drawing.Size(55, 40);" + Environment.NewLine;
            ketqua = ketqua + "this.btnSave.Text = \"Ghi\";" + Environment.NewLine;
            //ketqua = ketqua + "this.btnSave.TextAlign = System.Drawing.ContentAlignment.BottomCenter;" +
            //         Environment.NewLine;
            ketqua = ketqua + "this.btnSave.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;" +
                     Environment.NewLine;
            ketqua = ketqua + "this.btnSave.Visible = false;" + Environment.NewLine;
            ketqua = ketqua + "this.btnSave.Click += new System.EventHandler(this.btnSave_Click);" + Environment.NewLine;
            ketqua = ketqua + "//  " + Environment.NewLine;
            ketqua = ketqua + "//  btnCancel" + Environment.NewLine;
            ketqua = ketqua + "//  " + Environment.NewLine;
            ketqua = ketqua + "this.btnCancel.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;" +
                     Environment.NewLine;
            ketqua = ketqua + "this.btnCancel.AutoSize = false;" + Environment.NewLine;
            ketqua = ketqua + "this.btnCancel.Name = \"btnCancel\";" + Environment.NewLine;
            ketqua = ketqua + "this.btnCancel.Size = new System.Drawing.Size(55, 40);" + Environment.NewLine;
            ketqua = ketqua + "this.btnCancel.Text = \"Hủy\";" + Environment.NewLine;
            ketqua = ketqua +
                     "this.btnCancel.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;" +
                     Environment.NewLine;
            ketqua = ketqua + "this.btnCancel.Visible = false;" + Environment.NewLine;
            ketqua = ketqua + "this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);" +
                     Environment.NewLine;
            ketqua = ketqua + "//  " + Environment.NewLine;
            ketqua = ketqua + "//  toolStripSeparator1" + Environment.NewLine;
            ketqua = ketqua + "//  " + Environment.NewLine;
            ketqua = ketqua + "this.toolStripSeparator1.AutoSize = false;" + Environment.NewLine;
            ketqua = ketqua + "this.toolStripSeparator1.Name = \"toolStripSeparator1\";" + Environment.NewLine;
            ketqua = ketqua + "this.toolStripSeparator1.Size = new System.Drawing.Size(6, 40);" + Environment.NewLine;
            ketqua = ketqua + "//  " + Environment.NewLine;
            ketqua = ketqua + "//  btnClose" + Environment.NewLine;
            ketqua = ketqua + "//  " + Environment.NewLine;
            ketqua = ketqua + "this.btnClose.AutoSize = false;" + Environment.NewLine;
            ketqua = ketqua + "this.btnClose.Name = \"btnClose\";" + Environment.NewLine;
            ketqua = ketqua + "this.btnClose.Size = new System.Drawing.Size(55, 40);" + Environment.NewLine;
            ketqua = ketqua + "this.btnClose.Text = \"Thoát\";" + Environment.NewLine;
            ketqua = ketqua + "this.btnClose.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;" +
                     Environment.NewLine;
            ketqua = ketqua + "this.btnClose.Click += new System.EventHandler(this.btnClose_Click);" +
                     Environment.NewLine;


            ketqua = ketqua + "//" + Environment.NewLine;
            ketqua = ketqua + "// gridControl1" + Environment.NewLine;
            ketqua = ketqua + "//" + Environment.NewLine;

            ketqua = ketqua + "this.gridControl1.Cursor = System.Windows.Forms.Cursors.Default;" + Environment.NewLine;
            ketqua = ketqua + "this.gridControl1.Dock = System.Windows.Forms.DockStyle.Fill;" + Environment.NewLine;
            ketqua = ketqua + "this.gridControl1.Location = new System.Drawing.Point(0, 42);" + Environment.NewLine;
            ketqua = ketqua + "this.gridControl1.MainView = this.gridView1;" + Environment.NewLine;
            ketqua = ketqua + "this.gridControl1.Name = \"gridControl1\";" + Environment.NewLine;


            #region sss
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (Convert.ToInt32(dt.Rows[i]["Repository"]) > 0)
                {
                    ketqua = ketqua +
                             "this.gridControl1.RepositoryItems.AddRange(new DevExpress.XtraEditors.Repository.RepositoryItem[] {" +
                             Environment.NewLine;
                    break;
                }
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (Convert.ToInt32(dt.Rows[i]["Repository"]) > 0)
                {
                    if (i == dt.Rows.Count - 1)
                    {
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + "});" +
                                 Environment.NewLine;
                    }
                    else
                    {
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + "," +
                                 Environment.NewLine;
                    }
                }
            } 
            #endregion

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (Convert.ToInt32(dt.Rows[i]["Repository"]) > 0)
                {
                    ketqua = ketqua.Substring(0, ketqua.Length - 1);
                    ketqua=ketqua+"});" + Environment.NewLine;
                    break;}
            }
            
            ketqua = ketqua + "this.gridControl1.Size = new System.Drawing.Size(863, 414);" + Environment.NewLine;
            ketqua = ketqua + "this.gridControl1.TabIndex = 23;" + Environment.NewLine;
            ketqua = ketqua +
                     "this.gridControl1.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] {" +
                     Environment.NewLine;
            ketqua = ketqua + "this.gridView1});" + Environment.NewLine;
            ketqua = ketqua + "//" + Environment.NewLine;
            ketqua = ketqua + "// gridView1" + Environment.NewLine;
            ketqua = ketqua + "//" + Environment.NewLine;
            ketqua = ketqua + "this.gridView1.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {" +
                     Environment.NewLine;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i == dt.Rows.Count - 1)
                {
                    ketqua = ketqua + "this." + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) + "});" +
                             Environment.NewLine;
                }
                else
                {
                    ketqua = ketqua + "this." + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) + "," +
                             Environment.NewLine;
                }
            }

            ketqua = ketqua + "this.gridView1.GridControl = this.gridControl1;" + Environment.NewLine;
            ketqua = ketqua + "this.gridView1.Name = \"gridView1\";" + Environment.NewLine;
            ketqua = ketqua + "this.gridView1.OptionsBehavior.Editable = false;" + Environment.NewLine;
            ketqua = ketqua + "this.gridView1.OptionsBehavior.EditingMode = DevExpress.XtraGrid.Views.Grid.GridEditingMode.EditFormInplace;" + Environment.NewLine;
            ketqua = ketqua + "this.gridView1.NewItemRowText = \"Nhập thêm mới ở đây\";" + Environment.NewLine;
            ketqua = ketqua + "this.gridView1.OptionsEditForm.EditFormColumnCount = 2;" + Environment.NewLine;
            ketqua = ketqua +
                     "this.gridView1.OptionsEditForm.ShowUpdateCancelPanel = DevExpress.Utils.DefaultBoolean.False;" +
                     Environment.NewLine;

            ketqua = ketqua + "this.gridView1.OptionsSelection.MultiSelect = true;" + Environment.NewLine;
            //ketqua = ketqua + "this.gridView1.OptionsView.ColumnAutoWidth = false;" + Environment.NewLine;
            ketqua = ketqua + "this.gridView1.OptionsView.ShowFooter = true;" + Environment.NewLine;
            ketqua = ketqua + "this.gridView1.OptionsView.ShowGroupPanel = false;" + Environment.NewLine;
            ketqua = ketqua +
                     "this.gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(this.gridView1_FocusedRowChanged);" +
                     Environment.NewLine;
            ketqua = ketqua +
                     "this.gridView1.ValidateRow += new DevExpress.XtraGrid.Views.Base.ValidateRowEventHandler(this.gridView1_ValidateRow);" +
                     Environment.NewLine;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ketqua = ketqua + "//" + Environment.NewLine;
                ketqua = ketqua + "// " + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) + Environment.NewLine;
                ketqua = ketqua + "//" + Environment.NewLine;
                ketqua = ketqua + "this." + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) + ".Caption = \"" +
                         dt.Rows[i]["ColumnName"] + "\";" + Environment.NewLine;
                ketqua = ketqua + "this." + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) + ".FieldName = \"" +
                         dt.Rows[i]["ColumnName"] + "\";" + Environment.NewLine;
                ketqua = ketqua + "this." + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) + ".Name = \"" +
                         ReturnColName(dt.Rows[i]["ColumnName"].ToString()) + "\";" + Environment.NewLine;
                if (Convert.ToBoolean(dt.Rows[i]["Hidden"]) == true)
                {
                    //ketqua = ketqua + "this." + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) + ".Visible = false;" + Environment.NewLine;
                    //ketqua = ketqua + "this." + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) + ".VisibleIndex = 0;" + Environment.NewLine;
                }
                else
                {
                    ketqua = ketqua + "this." + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) + ".Visible = true;" +
                             Environment.NewLine;
                    ketqua = ketqua + "this." + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) + ".VisibleIndex = " +
                             i + ";" + Environment.NewLine;
                    ketqua = ketqua + "this." + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) + ".Width = 75;" +
                             Environment.NewLine;
                }

                switch (Convert.ToInt16(dt.Rows[i]["Repository"]))
                {
                    case 0: //không dùng
                        break;
                    case 1: //RepositoryItemDateEdit
                        ketqua = ketqua + "this." + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".ColumnEdit = this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ";" +
                                 Environment.NewLine;
                        ketqua = ketqua + "//" + Environment.NewLine;
                        ketqua = ketqua + "//" + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 Environment.NewLine;
                        ketqua = ketqua + "//" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".AutoHeight = false;" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {" +
                                 Environment.NewLine;
                        ketqua = ketqua +
                                 "new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});" +
                                 Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".CalendarTimeProperties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {" +
                                 Environment.NewLine;
                        ketqua = ketqua +
                                 "new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});" +
                                 Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ".Name = \"" +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + "\";" + Environment.NewLine;
                        break;
                    case 2: //RepositoryItemLookUpEdit
                        //ketqua = ketqua + "this.item" + dt.Rows[i]["ColumnName"].ToString() +
                        //         " = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();" +
                        //         Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".ColumnEdit = this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ";" +
                                 Environment.NewLine;

                        ketqua = ketqua + "//" + Environment.NewLine;
                        ketqua = ketqua + "//" + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 Environment.NewLine;
                        ketqua = ketqua + "//" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".AutoHeight = false;" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {" +
                                 Environment.NewLine;
                        ketqua = ketqua +
                                 "new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});" +
                                 Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {" +
                                 Environment.NewLine;
                        ketqua = ketqua + "new DevExpress.XtraEditors.Controls.LookUpColumnInfo(\"Name\", \"Name\")});" +
                                 Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".DisplayMember = \"Name\";" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ".Name = \"" +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + "\";" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".NullText = \"\";" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".ValueMember = \"ID\";" + Environment.NewLine;

                        break;
                    case 3: //RepositoryItemGridLookUpEdit
                        ketqua = ketqua + "this." + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".ColumnEdit = this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ";" +
                                 Environment.NewLine;
                        ketqua = ketqua + "//" + Environment.NewLine;
                        ketqua = ketqua + "//" + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 Environment.NewLine;
                        ketqua = ketqua + "//" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".AutoHeight = false;" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {" +
                                 Environment.NewLine;
                        ketqua = ketqua +
                                 "new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});" +
                                 Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".DisplayMember = \"Name\";" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ".Name = \"" +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + "\";" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".NullText = \"\";" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".ValueMember = \"ID\";" + Environment.NewLine;
                        ketqua = ketqua + "//" + Environment.NewLine;
                        ketqua = ketqua + "// grv" + dt.Rows[i]["ColumnName"].ToString() + Environment.NewLine;
                        ketqua = ketqua + "//" + Environment.NewLine;
                        ketqua = ketqua + "this.grv" + dt.Rows[i]["ColumnName"].ToString() +
                                 ".FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;" +
                                 Environment.NewLine;
                        ketqua = ketqua + "this.grv" + dt.Rows[i]["ColumnName"].ToString() + ".Name = \"grv" +
                                 dt.Rows[i]["ColumnName"].ToString() + "\";" + Environment.NewLine;
                        ketqua = ketqua + "this.grv" + dt.Rows[i]["ColumnName"].ToString() +
                                 ".OptionsSelection.EnableAppearanceFocusedCell = false;" + Environment.NewLine;
                        ketqua = ketqua + "this.grv" + dt.Rows[i]["ColumnName"].ToString() +
                                 ".OptionsView.ShowGroupPanel = false;" + Environment.NewLine;
                        break;
                    case 4: //RepositoryItemCalcEdit
                        //ketqua = ketqua + "this.item" + dt.Rows[i]["ColumnName"].ToString() +
                        //         " = new DevExpress.XtraEditors.Repository.RepositoryItemCalcEdit();" +
                        //         Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".ColumnEdit = this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ";" +
                                 Environment.NewLine;
                        ketqua = ketqua + "//" + Environment.NewLine;
                        ketqua = ketqua + "//" + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 Environment.NewLine;
                        ketqua = ketqua + "//" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".AutoHeight = false;" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {" +
                                 Environment.NewLine;
                        ketqua = ketqua +
                                 "new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});" +
                                 Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".DisplayFormat.FormatString = \"{0:N2}\";" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ".Name = \"" +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + "\";" + Environment.NewLine;
                        break;
                    case 5: //RepositoryItemButtonEdit
                        //ketqua = ketqua + "this.item" + dt.Rows[i]["ColumnName"].ToString() +
                        //         " = new DevExpress.XtraEditors.Repository.RepositoryItemCalcEdit();" +
                        //         Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnColName(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".ColumnEdit = this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ";" +
                                 Environment.NewLine;
                        ketqua = ketqua + "//" + Environment.NewLine;
                        ketqua = ketqua + "//" + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 Environment.NewLine;
                        ketqua = ketqua + "//" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".AutoHeight = false;" + Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) +
                                 ".Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {" +
                                 Environment.NewLine;
                        ketqua = ketqua +
                                 "new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});" +
                                 Environment.NewLine;
                        ketqua = ketqua + "this." + ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ".Name = \"" +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + "\";" + Environment.NewLine;
                        break;
                }
            }
            ketqua = ketqua + "//" + Environment.NewLine;
            ketqua = ketqua + "//" + FRMTableName(tablename) + Environment.NewLine;
            ketqua = ketqua + "//" + Environment.NewLine;
            ketqua = ketqua + "this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);" + Environment.NewLine;
            ketqua = ketqua + "this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;" + Environment.NewLine;
            ketqua = ketqua + "this.ClientSize = new System.Drawing.Size(863, 456);" + Environment.NewLine;
            ketqua = ketqua + "this.Controls.Add(this.gridControl1);" + Environment.NewLine;
            ketqua = ketqua + "this.Controls.Add(this.mnuMenu);" + Environment.NewLine;
            
            //ketqua = ketqua + "this.Controls.Add(this.barDockControlTop);" + Environment.NewLine;
            ketqua = ketqua + "this.Name = \"" + FRMTableName(tablename) + "\";" + Environment.NewLine;
            ketqua = ketqua + "this.Text = \"" + FRMTableName(tablename) + "\";" + Environment.NewLine;
            ketqua = ketqua + "this.Load += new System.EventHandler(this." + FRMTableName(tablename) + "_Load);" + Environment.NewLine;
            ketqua = ketqua + "this.mnuMenu.ResumeLayout(false);" + Environment.NewLine;
            ketqua = ketqua + "this.mnuMenu.PerformLayout();" + Environment.NewLine;
            // ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this.barManager1)).EndInit();" + Environment.NewLine;
            ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this.gridControl1)).EndInit();" +
                     Environment.NewLine;
            ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this.gridView1)).EndInit();" +
                     Environment.NewLine;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //if (Convert.ToInt16(dt.Rows[i]["Repository"]) > 0)
                //{
                //    ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this.item" +
                //             dt.Rows[i]["ColumnName"].ToString() + ")).EndInit();" + Environment.NewLine;
                //}
                switch (Convert.ToInt16(dt.Rows[i]["Repository"]))
                {
                    case 0: //không dùng
                        break;
                    case 1: //RepositoryItemDateEdit
                        ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this." +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ")).EndInit();" +
                                 Environment.NewLine;
                        break;
                    case 2: //RepositoryItemLookUpEdit
                        ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this." +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ")).EndInit();" +
                                 Environment.NewLine;
                        break;
                    case 3: //RepositoryItemGridLookUpEdit
                        ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this." +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ")).EndInit();" +
                                 Environment.NewLine;
                        break;
                    case 4: //RepositoryItemCalcEdit
                        ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this." +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ")).EndInit();" +
                                 Environment.NewLine;
                        break;
                    case 5: //RepositoryItemButtonEdit
                        ketqua = ketqua + "((System.ComponentModel.ISupportInitialize)(this." +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ")).EndInit();" +
                                 Environment.NewLine;
                        break;
                }
            }
            ketqua = ketqua + "this.SuspendLayout();" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "#endregion" + Environment.NewLine;
            ketqua = ketqua + Environment.NewLine;
            ketqua = ketqua + "private System.Windows.Forms.ToolStrip mnuMenu;" + Environment.NewLine;
            ketqua = ketqua + "private System.Windows.Forms.ToolStripButton btnNew;" + Environment.NewLine;
            ketqua = ketqua + "private System.Windows.Forms.ToolStripButton btnEdit;" + Environment.NewLine;
            ketqua = ketqua + "private System.Windows.Forms.ToolStripButton btnDelete;" + Environment.NewLine;
            ketqua = ketqua + "private System.Windows.Forms.ToolStripButton btnSave;" + Environment.NewLine;
            ketqua = ketqua + "private System.Windows.Forms.ToolStripButton btnCancel;" + Environment.NewLine;
            ketqua = ketqua + "private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;" +
                     Environment.NewLine;
            ketqua = ketqua + "private System.Windows.Forms.ToolStripButton btnClose;" + Environment.NewLine;
            ketqua = ketqua + "private DevExpress.XtraGrid.GridControl gridControl1;" + Environment.NewLine;
            ketqua = ketqua + "private DevExpress.XtraGrid.Views.Grid.GridView gridView1;" + Environment.NewLine;
            for (int i = 0; i < dt.Rows.Count; i++)
            {ketqua = ketqua + "private DevExpress.XtraGrid.Columns.GridColumn " +
                         ReturnColName(dt.Rows[i]["ColumnName"].ToString()) + ";" + Environment.NewLine;
                switch (Convert.ToInt16(dt.Rows[i]["Repository"]))
                {
                    case 0: //không dùng
                        break;
                    case 1: //RepositoryItemDateEdit
                        ketqua = ketqua + "private DevExpress.XtraEditors.Repository.RepositoryItemDateEdit " +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ";" + Environment.NewLine;
                        break;
                    case 2: //RepositoryItemLookUpEdit
                        ketqua = ketqua + "private DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit " +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ";" + Environment.NewLine;
                        break;
                    case 3: //RepositoryItemGridLookUpEdit
                        ketqua = ketqua + "private DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit " +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ";" + Environment.NewLine;
                        break;
                    case 4: //RepositoryItemCalcEdit
                        ketqua = ketqua + "private DevExpress.XtraEditors.Repository.RepositoryItemCalcEdit " +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ";" + Environment.NewLine;
                        break;
                    case 5: //RepositoryItemButtonEdit
                        ketqua = ketqua + "private DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit " +
                                 ReturnEditColumn(dt.Rows[i]["ColumnName"].ToString()) + ";" + Environment.NewLine;
                        break;
                }
            }
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;

            return ketqua;
        }

        public static string CodeForm(string table, string nam_space)
        {
            string ketqua = "";
            ketqua = ketqua + "using System;" + Environment.NewLine;
            ketqua = ketqua + "using System.Collections.Generic;" + Environment.NewLine;
            ketqua = ketqua + "using System.ComponentModel;" + Environment.NewLine;
            ketqua = ketqua + "using System.Data;" + Environment.NewLine;
            ketqua = ketqua + "using System.Drawing;" + Environment.NewLine;
            ketqua = ketqua + "using System.Text;" + Environment.NewLine;
            ketqua = ketqua + "using System.Linq;" + Environment.NewLine;
            ketqua = ketqua + "using System.Threading.Tasks;" + Environment.NewLine;
            ketqua = ketqua + "using System.Windows.Forms;" + Environment.NewLine;
            ketqua = ketqua + "using DevExpress.XtraEditors;" + Environment.NewLine;
            ketqua = ketqua + "using DevExpress.XtraGrid;" + Environment.NewLine;
            ketqua = ketqua + "using DevExpress.XtraGrid.Views.Grid;" + Environment.NewLine;
            ketqua = ketqua + "using TienIch;" + Environment.NewLine;
            ketqua = ketqua + "using TienIch.Business;" + Environment.NewLine;
            ketqua = ketqua + "using TienIch.Model;" + Environment.NewLine;
            ketqua = ketqua + "using TienIch.Utils;" + Environment.NewLine;
            ketqua = ketqua + Environment.NewLine;
            ketqua = ketqua + "namespace " + nam_space + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "\tpublic partial class " + FRMTableName(table) + " : Form" +
                     Environment.NewLine;
            ketqua = ketqua + "\t{" + Environment.NewLine;
            ketqua = ketqua + "\t\tDataTable m_DataSource = new DataTable();" + Environment.NewLine;
            ketqua = ketqua + "\t\tpublic " + FRMTableName(table) + "()" + Environment.NewLine;
            ketqua = ketqua + "\t\t{" + Environment.NewLine;
            ketqua = ketqua + "\t\t\tInitializeComponent();" + Environment.NewLine;
            ketqua = ketqua + "\t\t}" + Environment.NewLine;


            ketqua = ketqua + "\t\t#region Methods" + Environment.NewLine;

            ketqua = ketqua + "\t\tpublic static void SelectAll(ref DataTable dt, GridControl grd)" + Environment.NewLine;
            ketqua = ketqua + "\t\t{" + Environment.NewLine;
            ketqua = ketqua + "try" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "\t\t\tdt = " + table + "BO.Instance.FindAll();" + Environment.NewLine;
            ketqua = ketqua + "\t\t\t\t\tgrd.DataSource = dt;" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "catch (Exception ex)" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "    XtraMessageBox.Show(ex.Message);" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "\t\t}" + Environment.NewLine;

            ketqua = ketqua + "public static void Save(ref DataTable dt)" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "try" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;

            ketqua = ketqua + "DataTable dtRemine;" + Environment.NewLine;
            ketqua = ketqua + "dtRemine = dt.GetChanges();" + Environment.NewLine;
            ketqua = ketqua + "if (dtRemine != null)" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "for (int i = 0; i < dtRemine.Rows.Count; i++)" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "if (dtRemine.Rows[i][\"ID\"] == DBNull.Value) //add new" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "//code in here" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "else //update" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "//code in here" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
             ketqua = ketqua + "catch (Exception ex)" + Environment.NewLine;
             ketqua = ketqua + "{" + Environment.NewLine;
             ketqua = ketqua + "    XtraMessageBox.Show(ex.Message);" + Environment.NewLine;
             ketqua = ketqua + "}" + Environment.NewLine;

            ketqua = ketqua + "}" + Environment.NewLine;

            ketqua = ketqua + "\t\t#endregion" + Environment.NewLine;



            ketqua = ketqua + "\t\tprivate void " + FRMTableName(table) + "_Load(object sender, EventArgs e)" +
                    Environment.NewLine;
            ketqua = ketqua + "\t\t{" + Environment.NewLine;
            ketqua = ketqua + "\t\t\tSelectAll(ref m_DataSource, gridControl1);" + Environment.NewLine;
            ketqua = ketqua + "\t\t\tPageBase.StateButton(false, btnNew, btnEdit, btnSave, btnCancel, btnDelete);" +
                     Environment.NewLine;
            ketqua = ketqua + "\t\t\tPageBase.VisibleColumnInGrid(gridView1, false);" + Environment.NewLine;
            ketqua = ketqua + "\t\t}" + Environment.NewLine;
            ketqua = ketqua +
                     "\t\tprivate void btnNew_Click(object sender, EventArgs e)" +
                     Environment.NewLine;
            ketqua = ketqua + "\t\t{" + Environment.NewLine;
            ketqua = ketqua +
                     "\t\t\tgridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;" +
                     Environment.NewLine;
            ketqua = ketqua + "\t\t\tPageBase.StateButton(true, btnNew, btnEdit, btnSave, btnCancel, btnDelete);" +
                     Environment.NewLine;
            ketqua = ketqua + "\t\t\tPageBase.VisibleColumnInGrid(gridView1, true);" + Environment.NewLine;
            ketqua = ketqua + "\t\t\tGlobal.sFormStatus = Global.FormStatus.Addnew;" + Environment.NewLine;
            ketqua = ketqua + "gridView1.FocusedRowHandle = GridControl.NewItemRowHandle;" + Environment.NewLine;
            ketqua = ketqua + "//gridView1.FocusedColumn = colTenCV;" + Environment.NewLine;
            ketqua = ketqua + "gridView1.ShowEditor();" + Environment.NewLine;

            ketqua = ketqua + "\t\t}" + Environment.NewLine;
            ketqua = ketqua + "\t\tprivate void btnEdit_Click(object sender, EventArgs e)" +
                     Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "PageBase.StateButton(true, btnNew, btnEdit, btnSave, btnCancel, btnDelete);" +
                     Environment.NewLine;
            ketqua = ketqua + "PageBase.VisibleColumnInGrid(gridView1, true);" + Environment.NewLine;
            ketqua = ketqua + "Global.sFormStatus = Global.FormStatus.Edit;" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "private void btnSave_Click(object sender, EventArgs e)" +
                     Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "gridView1.FocusedRowHandle = -1;" + Environment.NewLine;
            ketqua = ketqua + "Save(ref m_DataSource);" + Environment.NewLine;
            ketqua = ketqua +
                     "gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;" +
                     Environment.NewLine;
            ketqua = ketqua + "PageBase.StateButton(false, btnNew, btnEdit, btnSave, btnCancel, btnDelete);" +
                     Environment.NewLine;
            ketqua = ketqua + "PageBase.VisibleColumnInGrid(gridView1, false);" + Environment.NewLine;
            ketqua = ketqua + "Global.sFormStatus = Global.FormStatus.View;" + Environment.NewLine;
            ketqua = ketqua + "SelectAll(ref m_DataSource, gridControl1);" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua +
                     " private void btnCancel_Click(object sender, EventArgs e)" +
                     Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "SelectAll(ref m_DataSource, gridControl1);" + Environment.NewLine;
            ketqua = ketqua + "PageBase.StateButton(false, btnNew, btnEdit, btnSave, btnCancel, btnDelete);" +
                     Environment.NewLine;
            ketqua = ketqua + "PageBase.VisibleColumnInGrid(gridView1, false);" + Environment.NewLine;
            ketqua = ketqua +
                     "gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;" +
                     Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua +
                     "private void btnDelete_Click(object sender, EventArgs e)" +
                     Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;


            ketqua = ketqua + "try" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "if (gridView1.FocusedRowHandle < 0)" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua +
                     "XtraMessageBox.Show(\"Không có mẫu tin nào để xóa\", \"Thông báo\", MessageBoxButtons.OK);" +
                     Environment.NewLine;
            ketqua = ketqua + "return;" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "//int IDs = Convert.ToInt32(gridView1.GetFocusedRowCellValue(colID).ToString());" + Environment.NewLine;
            ketqua = ketqua + "//if ("+table+"BO.Instance.CheckExist(\"SanPhamDAID\", IDs))" +
                     Environment.NewLine;
            ketqua = ketqua + "//{" + Environment.NewLine;
            ketqua = ketqua +
                     "//XtraMessageBox.Show(\"Mẫu tin này đang được sử dụng nên không xóa được\", \"Thông báo\", MessageBoxButtons.OK);" +
                     Environment.NewLine;
            ketqua = ketqua + "//return;" + Environment.NewLine;
            ketqua = ketqua + "//}" + Environment.NewLine;
          
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua +
                     "if (XtraMessageBox.Show(\"Bạn có chắc xóa mẫu tin này không?\", \"Thông báo\", MessageBoxButtons.YesNo) == DialogResult.Yes)" +
                     Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;

            ketqua = ketqua + "int j = -1;" + Environment.NewLine;
            ketqua = ketqua + "for (int i = 0; i < gridView1.SelectedRowsCount; i++)" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "    if (gridView1.GetSelectedRows()[i] >= 0)" + Environment.NewLine;
            ketqua = ketqua + "        j = gridView1.GetSelectedRows()[i];" + Environment.NewLine;
            ketqua = ketqua + "    if (j >= 0)" + Environment.NewLine;
            ketqua = ketqua + "    {" + Environment.NewLine;
            ketqua = ketqua + "        int IDs = Convert.ToInt32(gridView1.GetRowCellValue(j, colID).ToString());" + Environment.NewLine;
            ketqua = ketqua + "        //if (HangMucCongViecBO.Instance.CheckExist(\"SanPhamDAID\", IDs))" + Environment.NewLine;
            ketqua = ketqua + "        //{" + Environment.NewLine;
            ketqua = ketqua + "            //XtraMessageBox.Show(\"Mẫu tin này đang được sử dụng, bạn không thể xóa nó\", \"Thông báo\");" + Environment.NewLine;
            ketqua = ketqua + "            //return;" + Environment.NewLine;
            ketqua = ketqua + "        //}" + Environment.NewLine;
            ketqua = ketqua + "        "+table+"BO.Instance.Delete(IDs);" + Environment.NewLine;
            ketqua = ketqua + "    }" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "SelectAll(ref m_DataSource, gridControl1);" + Environment.NewLine;

            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;

            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "catch (Exception ex)" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "    XtraMessageBox.Show(ex.Message);" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;

            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "private void btnClose_Click(object sender, EventArgs e)" +
                     Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "\tthis.Close();" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua +
                     "private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)" +
                     Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "try" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "if (e.FocusedRowHandle >= 0 & Global.sFormStatus == Global.FormStatus.View)" +
                     Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "Global.sIDLinkGridView = gridView1.GetFocusedRowCellValue(\"ID\").ToString();" +
                     Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;


            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "catch (Exception ex)" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "    XtraMessageBox.Show(ex.Message);" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;

            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua +
                     "private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)" +
                     Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "try" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "//if (gridView1.GetRowCellValue(e.RowHandle, \"Name\").ToString() == \"\")" +
                     Environment.NewLine;
            ketqua = ketqua + "//{" + Environment.NewLine;
            ketqua = ketqua +
                     "//XtraMessageBox.Show(\"AAAAAAAAAAAAAA không được rỗng\", \"Thông báo\", MessageBoxButtons.OK);" +
                     Environment.NewLine;
            ketqua = ketqua + "//e.Valid = false;" + Environment.NewLine;
            ketqua = ketqua + "//}" + Environment.NewLine;

            ketqua = ketqua + "GridView view = sender as GridView;" + Environment.NewLine;








            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "catch (Exception ex)" + Environment.NewLine;
            ketqua = ketqua + "{" + Environment.NewLine;
            ketqua = ketqua + "    XtraMessageBox.Show(ex.Message);" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;

            ketqua = ketqua + "//SelectAll(ref m_DataSource, gridControl1);" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;
            ketqua = ketqua + "}" + Environment.NewLine;

            return ketqua;
        }

        public static void FillRepository(DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit cb)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof (int));
            dt.Columns.Add("Name", typeof (string));
            DataRow dr = dt.NewRow();
            dr["ID"] = 0;
            dr["Name"] = "Không dùng";
            dt.Rows.Add(dr);
            DataRow dr1 = dt.NewRow();
            dr1["ID"] = 1;
            dr1["Name"] = "Kiểu ngày tháng";
            dt.Rows.Add(dr1);
            DataRow dr2 = dt.NewRow();
            dr2["ID"] = 2;
            dr2["Name"] = "Kiểu LookUpEdit";
            dt.Rows.Add(dr2);
            DataRow dr3 = dt.NewRow();
            dr3["ID"] = 3;
            dr3["Name"] = "Kiểu GridLookUpEdit";
            dt.Rows.Add(dr3);
            DataRow dr4 = dt.NewRow();
            dr4["ID"] = 4;
            dr4["Name"] = "Kiểu CalEdit";
            dt.Rows.Add(dr4);
            DataRow dr5 = dt.NewRow();
            dr5["ID"] = 5;
            dr5["Name"] = "Kiểu ButtonEdit";
            dt.Rows.Add(dr5);
            cb.DataSource = dt;
        }

        private static void CreateTableStructure(ref DataTable dt)
        {
            dt = new DataTable();
            dt.Columns.Add("ColumnName", typeof (string));
            dt.Columns.Add("Hidden", typeof (bool));
            dt.Columns.Add("Repository", typeof (int));
        }

        private static string FRMTableName(string tablename)
        {
            return "frm" +
                   System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(tablename.ToLower());
        }
        private static string ReturnColName(string columnname)
        {
            return "col" + columnname;
        }

        private static string ReturnEditColumn(string columnname)
        {
            return "item" + columnname;
        }

        private static string BUSTableName(string tablename)
        {
            return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(tablename.ToLower()) +
                   "BUS";
        }
    }}
